/**
 * @file   : loopHelper.ts
 * @time   : 14:44
 * @date   : 2025/4/1
 * @mail   : 9727005@qq.com
 * @creator: ShanDong Xiedali
 * @company: Less is more. Simple is best!
 */

export class LoopHelper {
    /**
     * 缝合多个数组的第i个元素为一个新的元素，返回缝合后的新数组
     * @param arrays
     */
    static zip(...arrays: any[][]) {
        const arrayLength = arrays.map(function (arr) {
            return arr.length;
        });

        const minCount = Math.min(...arrayLength);
        let result = [];
        for (let i = 0; i < minCount; i++) {
            const temp: any[] = [];
            for (let j = 0; j < arrays.length; j++) {
                temp.push(arrays[j][i]);
            }
            result[i] = temp;
        }

        return result;
    }

    /**
     * 计算多个数组的笛卡尔积
     * @param arrays
     */
    static product(...arrays: any[][]): any[][] {
        let result: any[][] = [[]];
        arrays.forEach(array => {
            const temp: any[][] = [];
            result.forEach(product => {
                array.forEach(item => {
                    temp.push([...product, item]);
                });
            });
            result = temp;
        });
        return result;
    }
}
